<?php

namespace ball;
use GuzzleHttp\Client;
use think\facade\Cache;
use ball\upay\Upay;

/**
 * 收钱吧api
 */
class UpayMiniApp extends Upay
{
    protected $appid = '2025050900009028'; //'2025050900009027'
    protected $code = '92154836';
    protected $device_id = 'miniApp';
    protected int $refreshTokenKeepTime = 86400*15;

    public function __construct()
    {
        parent::__construct();
    }
    public function activate( array $terminal=[]){
        $config = [
            'app_id'    => $terminal['appid'],
            'code'      => $terminal['code'],
            'device_id' => $terminal['device_id'],
        ];
        $response = parent::activate($config);
        if($response !== false && $response['result_code'] == '200'){
            $result = $response['biz_response'];
            return $result;
        }
        
    }

    /*签到接口
     * "terminal_sn" => "100090280046174505"
     * "terminal_key" => "4688b3f17f2d648cb644e702e63b4675"
     * "merchant_sn" => "18956397746"
     * "merchant_name" => "半夜鸡叫"
     * "store_sn" => "00010101001200200046406"
     * "store_name" => "半夜鸡叫"
     * */
    public function checkin(array $terminal = [])
    {
        $url = $this->apiDomain. '/terminal/checkin';
        $params = [
            'terminal_sn' => $terminal['terminal_sn'],       //终端号
            'device_id' => $terminal['device_id'],
        ];                //设备唯一身份ID
        ksort($params);
        $response = $this->httpBuild($url,$params,$terminal['terminal_sn'],$terminal['terminal_key']);
        if($response !== false && $response['result_code'] == '200'){
            $result = $response['biz_response'];
            return $result;
        }
    }
    /**
     * 查单
     */
    public function query( string $out_trade_no='', array $terminal=[])
    {
        $url = $this->apiDomain. '/upay/v2/query';
    
        $params['terminal_sn'] = $terminal['terminal_sn'];      //收钱吧终端ID
        //$params['sn']='';         //收钱吧系统内部唯一订单号
        $params['client_sn'] = $out_trade_no;    //商户系统订单号,必须在商户系统内唯一；且长度不超过64字节
        ksort($params);
        $response = $this->httpBuild($url,$params,$terminal['terminal_sn'],$terminal['terminal_key']);
        if($response !== false && $response['result_code'] == '200'){
            $result = $response['biz_response'];
            return $result;
        }
    }
    /**
     * 预下单
     */
    public function precreate(array $order,  array $terminal)
    {
        $url = $this->apiDomain.'/upay/v2/precreate';
    
        $params['terminal_sn'] = $terminal['terminal_sn'];      //收钱吧终端ID
        $params['client_sn'] = $order['out_trade_no'];                  //商户系统订单号,必须在商户系统内唯一；且长度不超过64字节
        $params['total_amount'] = $order['order_amount'];               //交易总金额
        $params['payway']='3';                      //内容为数字的字符串	2:支付宝3:微信
        $params['subject'] = $order['title'];              //本次交易的概述
        $params['operator'] = $terminal['device_id'];             //发起本次交易的操作员
        $params['sub_payway']='4';           //内容为数字的字符串，如果要使用WAP支付，则必须传 "3", 使用小程序支付请传"4"
        $params['payer_uid']=$order['openid'];          //消费者在支付通道的唯一id,微信WAP支付必须传open_id,支付宝WAP支付必传用户授权的
        $params['notify_url']=$order['notify_url'];     //支付回调的地址
        ksort($params);
        $response = $this->httpBuild($url,$params,$terminal['terminal_sn'],$terminal['terminal_key']);
        if($response !== false && $response['result_code'] == '200'){
            $result = $response['biz_response'];
            return $result;
        }
        return false;
    }

    /*付款码
     */
    public function  pay(array $order,array $terminal)
    {
    
        $url = $this->apiDomain . "/upay/v2/pay";

        $params['terminal_sn'] = $terminal['terminal_sn'];             //终端号
        $params['client_sn'] = $order['out_trade_no'];    //商户系统订单号,必须在商户系统内唯一；且长度不超过64字节
        $params['total_amount'] = $order['order_amount']*100;                     //交易总金额,以分为单位
        //$params['payway'] = '1';                          //支付方式,1:支付宝 3:微信 4:百付宝 5:京东钱包
        $params['dynamic_id'] = '';       //条码内容（支付包或微信条码号）
        $params['subject'] = '';                        //交易简介
        $params['operator'] = $terminal['device_id']; 
    }
    /**
     * C扫B支付
     */
    public function wap_api_pro(array $order,array $terminal)
    {
        $params['terminal_sn'] = $terminal['terminal_sn'];     //收钱吧终端ID
        $params['client_sn'] = $order['out_trade_no'];    //商户系统订单号,必须在商户系统内唯一；且长度不超过64字节
        $params['total_amount'] = $order['order_amount']*100;             //以分为单位,不超过10位纯数字字符串,超过1亿元的收款请使用银行转账
        $params['subject'] = '';            //本次交易的概述
        $params['notify_url'] = $order['notify_url'];   //支付回调的地址
        $params['operator'] = $terminal['device_id'];                   //发起本次交易的操作员
        $params['return_url'] = $order['return_url'];  //处理完请求后，当前页面自动跳转到商户网站里指定页面的http路径
        $params['reflect'] =  //反射参数
        ksort($params);  //进行升序排序
        $param_str = "";
        foreach ($params as $k => $v) {
            $param_str .= $k .'='.$v.'&';
        }
        $sign = strtoupper(md5($param_str . 'key=' . $terminal['terminal_key']));

        $paramsStr = $param_str . "sign=" . $sign;
        $res = "https://qr.shouqianba.com/gateway?" . $paramsStr;
        return $res;
        //将这个url生成二维码扫码或在微信链接中打开可以完成测试
        //file_put_contents('logs/wap_api_pro_' . date('Y-m-d') . '.txt', $res, FILE_APPEND);
    }


}